package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
import com.alibaba.excel.metadata.CellData;
import com.example.demo.handler.CommentWriteHandler;
import com.example.demo.handler.SpinnerWriteHandler;
import com.example.demo.model.ExcelModel;
import com.example.demo.model.ExcelModelException;
import lombok.SneakyThrows;
import org.apache.commons.codec.binary.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author huangzhuoguang
 * @Description
 * @date 2021/1/8
 */
public class ReadExcelDemo {
    public static void main(String[] args) throws Exception {
        String path = "C:\\Users\\4K\\Downloads\\采购订单f21b6355-6594-48e2-bdb4-ee1b528e1de8.xlsx";
        List<ExcelModelException> list = new ArrayList<>();
        EasyExcel.read(path, ExcelModel.class, new AbstractIgnoreExceptionReadListener<ExcelModel>() {
            Integer i = 0;
            Integer row = 1;//记录有问题的行数

            @SneakyThrows
            @Override
            //检查标题头
            public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
                ExcelModel excelModel = new ExcelModel();
                Class aClass = excelModel.getClass();
                for (Field field : aClass.getDeclaredFields()) {
                    ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
                    System.out.println(field.isAccessible());
                    field.setAccessible(true);//暴力读取的意思
                    CellData cellData = headMap.get(i++);
                    if (!StringUtils.equals(cellData.toString(), annotation.value()[0])) {
//                        System.out.println("请检查表头是否正确");
                        list.add(new ExcelModelException("请检查表头是否正确"));
                    }
                }

            }

            //检查内容
            @Override
            public void invoke(ExcelModel data, AnalysisContext context) {
                System.out.println("data = " + data);
                if (com.alibaba.excel.util.StringUtils.isEmpty(data.getId())) {
                    list.add(new ExcelModelException("账号Id不为空，第" + row + "行"));
                }
                row++;
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("共有" + (row - 1) + "行");
            }
        }).sheet().doRead();
        String path1 = "C:\\Users\\4K\\Downloads\\采购订单错误信息.xlsx";
        if (list.size() > 0) {
//            EasyExcel.write(path1, ExcelModelException.class).sheet("采购订单").doWrite(list);
            EasyExcel.write(path1, ExcelModelException.class).sheet("采购订单")
                    //下拉框拦截器
                    .registerWriteHandler(new SpinnerWriteHandler())
                    //注册批注拦截器
                    .registerWriteHandler(new CommentWriteHandler())
                    .doWrite(list);
        }

    }
}
